class Comment {
  final int id;
  final int techniqueId;
  final int userId;
  final String content;
  final int likeCount;
  final String status;
  final String createTime; // 改为String类型，匹配后端
  final bool isLiked;
  final int? parentId; // 用于回复评论
  final UserInfo? user;
  final List<Comment>? replies;
  final String? targetTitle; // 评论的目标文章/帖子标题
  final int? targetId; // 评论的目标文章/帖子ID

  Comment({
    required this.id,
    required this.techniqueId,
    required this.userId,
    required this.content,
    required this.likeCount,
    required this.status,
    required this.createTime,
    this.isLiked = false,
    this.parentId,
    this.user,
    this.replies,
    this.targetTitle,
    this.targetId,
  });

  factory Comment.fromJson(Map<String, dynamic> json) {
    // 处理 id 字段，可能是字符串或整数
    int commentId = 0;
    if (json['id'] != null) {
      commentId = json['id'] is String ? int.tryParse(json['id']) ?? 0 : json['id'];
    }
    
    // 处理 userId 字段，可能是字符串或整数
    int commentUserId = 0;
    if (json['userId'] != null) {
      commentUserId = json['userId'] is String ? int.tryParse(json['userId']) ?? 0 : json['userId'];
    }
    
    // 处理 techniqueId/postId 字段
    int commentTechniqueId = 0;
    if (json['techniqueId'] != null) {
      commentTechniqueId = json['techniqueId'] is String ? int.tryParse(json['techniqueId']) ?? 0 : json['techniqueId'];
    } else if (json['postId'] != null) {
      commentTechniqueId = json['postId'] is String ? int.tryParse(json['postId']) ?? 0 : json['postId'];
    }
    
    // 处理 parentId 字段，可能是字符串或整数
    int? commentParentId;
    if (json['parentId'] != null) {
      commentParentId = json['parentId'] is String ? int.tryParse(json['parentId']) : json['parentId'];
    }
    
    // 创建用户信息对象
    UserInfo? userInfo;
    if (json['user'] != null) {
      userInfo = UserInfo.fromJson(json['user']);
    } else if (json['userName'] != null) {
      // 从 userName 和 userAvatar 字段创建用户信息
      userInfo = UserInfo(
        id: commentUserId,
        nickName: json['userName'] ?? '',
        avatar: json['userAvatar'],
      );
    }
    
    return Comment(
      id: commentId,
      techniqueId: commentTechniqueId,
      userId: commentUserId,
      content: json['content'] ?? '',
      likeCount: json['likeCount'] ?? 0,
      status: json['status'] ?? '1',
      createTime: json['createTime'] ?? '',
      isLiked: json['isLiked'] ?? false,
      parentId: commentParentId,
      user: userInfo,
      replies: json['replies'] != null 
          ? (json['replies'] as List).map((reply) => Comment.fromJson(reply)).toList()
          : null,
      targetTitle: json['targetTitle'],
      targetId: json['targetId'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'techniqueId': techniqueId,
      'userId': userId,
      'content': content,
      'likeCount': likeCount,
      'status': status,
      'createTime': createTime,
      'isLiked': isLiked,
      'parentId': parentId,
      'user': user?.toJson(),
      'replies': replies?.map((reply) => reply.toJson()).toList(),
      'targetTitle': targetTitle,
      'targetId': targetId,
    };
  }

  Comment copyWith({
    int? id,
    int? techniqueId,
    int? userId,
    String? content,
    int? likeCount,
    String? status,
    String? createTime,
    bool? isLiked,
    int? parentId,
    UserInfo? user,
    List<Comment>? replies,
    String? targetTitle,
    int? targetId,
  }) {
    return Comment(
      id: id ?? this.id,
      techniqueId: techniqueId ?? this.techniqueId,
      userId: userId ?? this.userId,
      content: content ?? this.content,
      likeCount: likeCount ?? this.likeCount,
      status: status ?? this.status,
      createTime: createTime ?? this.createTime,
      isLiked: isLiked ?? this.isLiked,
      parentId: parentId ?? this.parentId,
      user: user ?? this.user,
      replies: replies ?? this.replies,
      targetTitle: targetTitle ?? this.targetTitle,
      targetId: targetId ?? this.targetId,
    );
  }
}

class UserInfo {
  final int id;
  final String nickName;
  final String? avatar;

  UserInfo({
    required this.id,
    required this.nickName,
    this.avatar,
  });

  factory UserInfo.fromJson(Map<String, dynamic> json) {
    return UserInfo(
      id: json['id'],
      nickName: json['nickName'] ?? '',
      avatar: json['avatar'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'nickName': nickName,
      'avatar': avatar,
    };
  }
}